package com.weblearning.tliasweb.controller;

import com.weblearning.tliasweb.pojo.Result;
import com.weblearning.tliasweb.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    //本地存储方式
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws Exception {
//        log.info("上传文件"+username+" "+age+" "+image);
//        String imageName = image.getOriginalFilename();
//        String filename=UUID.randomUUID()+imageName.substring(imageName.lastIndexOf("."));
//        image.transferTo(new File("D:/黑马java资料\\day11-SpringBootWeb案例\\资料\\03. 文件上传\\"+filename));
//        return Result.success();
//    }

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传"+image.getOriginalFilename());
        String url=aliOSSUtils.upload(image);
        log.info("文件的url 为"+url);
        return Result.success(url);
    }


}
